package spider;

import org.json.JSONObject;
import wordsCloud.WordCloudUtils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class ScheduledTasks {

    public void scheduledTask() {
//    public static void main(String[] args) {
        JobList jobList = new JobList();
        String job_file_name = jobList.getJobList();
//        String job_file_name = "ruoyi-hzhtest/src/main/resources/job_list/job_list.txt";
        String outputFilePath = "ruoyi-hzhtest/src/main/resources/job_list/word_frequency.txt";
        Map<String, Integer> wordFrequency = new HashMap<>();

        // 读取输入文件
        try (BufferedReader reader = new BufferedReader(new FileReader(job_file_name))) {
            String line;
            while ((line = reader.readLine()) != null) {
                // 解析 JSON 数据
                JSONObject job = new JSONObject(line);
                if (job.has("skills")) {
                    for (Object skill : job.getJSONArray("skills")) {
                        String skillStr = skill.toString();
                        wordFrequency.put(skillStr, wordFrequency.getOrDefault(skillStr, 0) + 1);
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 将结果写入输出文件
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(outputFilePath))) {
            for (Map.Entry<String, Integer> entry : wordFrequency.entrySet()) {
                writer.write(entry.getKey() + " " + entry.getValue());
                writer.newLine();
            }
            System.out.println("词频统计已写入 " + outputFilePath + " 文件。");
        } catch (IOException e) {
            e.printStackTrace();
        }

        WordCloudUtils w = new WordCloudUtils();
        w.createSimpleImage(outputFilePath);
    }

}
